<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <style>
            .selected_class{
                box-shadow: 3px 3px 6px rgba(85, 154, 227, 0.4);
                border: 2px solid #2d8cf0;
            }
            .ellipsis {
                overflow: hidden; /* 确保超出容器的文本被隐藏 */
                white-space: nowrap; /* 确保文本在一行内显示 */
                text-overflow: ellipsis; /* 使用省略号表示被截断的文本 */
            }
        </style>
    </head>
    <body>

        <form class="layui-form" action="">

        <div class="mainBox">
            <div class="main-container mr-5">
                <div class="layui-form-item">
                    <label class="layui-form-label required">方案名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" placeholder="" lay-verify="required" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">语言</label>
                    <div class="layui-input-block">
                        <div name="lang" id="lang" value="" ></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">封面图</label>
                    <div class="layui-input-block">
                        <img class="img-3" src=""/>
                        <input type="text" style="display:none" name="image" value="" lay-verify="required"/>
                        <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="image"  permission="app.admin.upload.image">
                            <i class="layui-icon layui-icon-upload"></i>上传图片
                        </button>
                        <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="attachment-choose-image_path"  permission="app.admin.upload.attachment">
                            <i class="layui-icon layui-icon-align-left"></i>选择图片
                        </button>
                    </div>
                </div>
                <!--<div class="layui-form-item">-->
                    <!--<label class="layui-form-label required">创作内容</label>-->
                    <!--<div class="layui-input-block">-->
                        <!--<div name="chapterid" id="chapterid" value=""></div>-->
                    <!--</div>-->
                <!--</div>-->
                <div class="layui-form-item">
                    <label class="layui-form-label required">素材选择</label>
                    <div class="layui-input-block material_html"  style="margin-right:10px;display: flex;flex-direction: row;align-items: center;flex-wrap: wrap;">
                        <div class="font_click"  data-id="1" style="width: 100px;height: 120px;margin-right: 10px;">
                            <img src="https://ai-video-1320718644.cos.ap-guangzhou.myqcloud.com/video/local/stable_diffusion_image/20240513/png/0b52503b7725942befdf.png" style="width: 100px;height: 100px;">
                            <div>实打实大苏打</div>
                        </div>
                    </div>
                    <input type="hidden" name="material_id" value="" lay-verify="required">
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">主播音色</label>
                    <div class="layui-input-block">
                        <div name="timbreid" id="timbreid" value=""></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">主播音量</label>
                    <div class="layui-input-block">
                        <input type="number" style="display:none" name="timbre_volume" value="10" />
                        <div id="timbre_volume" style="top:15px;"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">主播语速</label>
                    <div class="layui-input-block">
                        <input type="number" style="display:none" name="speak_speed" value="10" />
                        <div id="speak_speed" style="top:15px;"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">字幕样式</label>
                    <div class="layui-input-block font_html"  style="margin-right:10px;display: flex;flex-direction: row;align-items: center;flex-wrap: wrap;">
                        <div class="font_click"  data-id="1" style="width: 100px;height: 120px;margin-right: 10px;">
                            <img src="https://ai-video-1320718644.cos.ap-guangzhou.myqcloud.com/video/local/stable_diffusion_image/20240513/png/0b52503b7725942befdf.png" style="width: 100px;height: 100px;">
                            <div>实打实大苏打</div>
                        </div>
                    </div>
                    <input type="hidden" name="fontid" value=""  lay-verify="required">
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">背景音乐</label>
                    <div class="layui-input-block">
                        <div name="musicid" id="musicid" value=""></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">音乐音量</label>
                    <div class="layui-input-block">
                        <input type="number" style="display:none" name="music_volume" value="5" />
                        <div id="music_volume" style="top:15px;"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">字幕位置</label>
                    <div class="layui-input-block" style="display: flex;flex-direction: row;align-items: center;">
                        <span>x</span><input type="text" name="caption_post_x" placeholder="x" value="0" lay-verify="required" class="layui-input" style="width: 30%">
                        <span style="margin: 0 10px">-</span>
                        <span>y</span><input type="text" name="caption_post_y" placeholder="y" value="0" lay-verify="required" class="layui-input" style="width: 30%">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">字幕大小</label>
                    <div class="layui-input-block">
                        <input type="number" style="display:none" name="font_size" value="20" />
                        <div id="font_size" style="top:15px;"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">视频比例</label>
                    <div class="layui-input-block">
                        <div name="video_scale" id="video_scale"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">去重设置</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="settings[random_reduce]" lay-skin="switch" lay-text="随机抽贞|随机抽贞" checked>  &nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="checkbox" name="settings[random_add]" lay-skin="switch" lay-text="随机补贞|随机补贞" checked>  &nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="checkbox" name="settings[random_speed]" lay-skin="switch" lay-text="随机变速|随机变速" checked>  &nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="checkbox" name="settings[random_mirror]" lay-skin="switch" lay-text="随机镜像|随机镜像" checked>  &nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="checkbox" name="settings[random_hash]" lay-skin="switch" lay-text="MD5修改|MD5修改" checked>  &nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="checkbox" name="settings[random_filter]" lay-skin="switch" lay-text="随机滤镜|随机滤镜" checked>  &nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="checkbox" name="settings[random_effect]" lay-skin="switch" lay-text="随机特效|随机特效" checked>  &nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="checkbox" name="settings[random_transition]" lay-skin="switch" lay-text="随机转场|随机转场" checked>  &nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="checkbox" name="settings[random_enlarge]" lay-skin="switch" lay-text="随机放大|随机放大" checked>  &nbsp;&nbsp;&nbsp;&nbsp;
                    </div>
                </div>
            </div>
        </div>

        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                        lay-filter="save">
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    重置
                </button>
            </div>
        </div>

    </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/functions.js"></script>
        <script src="/app/admin/admin/js/md5.js"></script>
        
        <script>
            let material_ids = [];
            let material_data = [];

            // 相关接口
            const PRIMARY_KEY = "id";
            const PRIMARY_KEY_VALUE = layui.url().search[PRIMARY_KEY];
            const SELECT_API = "/app/admin/plan/oversea/select" + location.search;
            const UPDATE_API = "/app/admin/plan/oversea/update";


            // 获取数据库记录
            layui.use(["form", "util", "popup", "xmSelect"], function () {
                $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    async:true,
                    headers: get_header({"id":PRIMARY_KEY_VALUE}),
                    success: function (res) {
                        
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            } else {
                                obj.attr("value", value);
                                obj[0].value = value;
                            }
                        });

                        // 字段 状态 lang
                        layui.use(["jquery", "xmSelect"], function() {
                            let value = layui.$("#lang").attr("value");
                            let initValue = value ? value.split(",") : ['en'];
                            layui.xmSelect.render({
                                el: "#lang",
                                name: "lang",
                                filterable: true,
                                initValue: initValue,
                                data: [{"value":"en","name":"英文"},{"value":"id","name":"印尼语"},{"value":"th","name":"泰语"}],
                                model: {"icon":"hidden","label":{"type":"text"}},
                                clickClose: true,
                                radio: true
                            })
                        });

                        // 字段 图片地址 image_path
                        layui.use(["upload", "layer"], function() {
                            let input = layui.$("#image").prev();
                            input.prev().attr("src", input.val());
                            layui.$("#attachment-choose-image_path").on("click", function() {
                                parent.layer.open({
                                    type: 2,
                                    title: "选择附件",
                                    content: "/app/admin/upload/attachment?ext=jpg,jpeg,png,gif,bmp",
                                    area: ["95%", "90%"],
                                    success: function (layero, index) {
                                        parent.layui.$("#layui-layer" + index).data("callback", function (data) {
                                            input.val(data.url).prev().attr("src", data.url);
                                        });
                                    }
                                });
                            });
                            layui.upload.render({
                                elem: "#image",
                                acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                                url: "/app/admin/upload/image",
                                field: "__file__",
                                headers: get_header({}),
                                done: function (res) {
                                    if (res.code > 0) return layui.layer.msg(res.msg);
                                    this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                                }
                            });
                        });

                        layui.use(["form", "util", "popup","jquery", "xmSelect"], function () {
                            // 字段 指令模板
                            layui.$.ajax({
                                url: "/app/admin/material/index/select?status=1&limit=200&fileter_empty=1",
                                dataType: "json",
                                async:true,
                                headers: get_header({"status":1,"fileter_empty":1}),
                                success: function (res2) {
                                    let html = '';
                                    material_data = res2.data;
                                    material_ids = res.data[0]['material_id'].split(',');
                                    layui.$.each(material_data,function (i,v) {
                                        let selected = '';
                                        if(material_ids.indexOf(String(v['id']))>-1){
                                            selected = 'selected_class';
                                        }
                                        html += ' <div class="material_click '+selected+'" data-id="'+v.id+'" style="width: 100px;height: 120px;margin-right: 10px;">\n' +
                                            '           <img src="'+v.image+'"  title="'+v.name+'" style="width: 100px;height: 100px;">\n' +
                                            '           <div class="ellipsis">'+v.name+'</div>\n' +
                                            '  </div>';
                                    })
                                    layui.$('.material_html').html(html);
                                    if (res2.code) {
                                        layui.popup.failure(res2.msg);
                                    }
                                }
                            });
                            $('.material_html').delegate('.material_click','click',function () {
                                let materialid = String($(this).data('id'));
                                let ishas = false;
                                layui.$.each(material_ids,function (i,v) {
                                    if(v==materialid){
                                        ishas = true;
                                        material_ids.splice(i,1);
                                    }
                                })
                                if(ishas==false){
                                    material_ids.push(materialid);
                                }

                                let tempIds = [];
                                layui.$.each(material_ids,function (i,v) {
                                    tempIds.push(v);
                                })
                                material_ids = tempIds

                                let html = '';
                                layui.$.each(material_data,function (i,v) {
                                    let selected = '';
                                    let materialid = String(v.id);
                                    if(material_ids.indexOf(materialid)>-1){
                                        selected = 'selected_class';
                                    }
                                    html += ' <div class="material_click '+selected+'" data-id="'+materialid+'" style="width: 100px;height: 120px;margin-right: 20px;">\n' +
                                        '           <img src="'+v.image+'" style="width: 100px;height: 100px;" title="'+v.name+'">\n' +
                                        '           <div class="ellipsis">'+v.name+'</div>\n' +
                                        '  </div>';
                                })
                                layui.$('.material_html').html(html);

                                layui.$('input[name="material_id"]').val(material_ids.join(','));
                            })

                        });

                        layui.use(["jquery", "slider"], function(){
                            let value = $('input[name="timbre_volume"]').val();
                            var slider = layui.slider;

                            //主播音量
                            slider.render({
                                elem: '#timbre_volume',  //绑定元素
                                min:1,
                                max:100,
                                step:1,
                                value: value!=undefined ? value : 10,
                                tips:true,
                                theme:"#2d8cf0",
                                input:true,
                                change: function(value){
                                    layui.$('input[name="timbre_volume"]').val(value);
                                    //console.log(value) //动态获取滑块数值
                                    //do something
                                }
                            });

                            value = $('input[name="speak_speed"]').val();
                            //主播语速
                            slider.render({
                                elem: '#speak_speed',  //绑定元素
                                min:5,
                                max:20,
                                step:1,
                                value: value!=undefined ? value : 10,
                                tips:true,
                                theme:"#2d8cf0",
                                input:true,
                                change: function(value){
                                    layui.$('input[name="speak_speed"]').val(value);
                                    //console.log(value) //动态获取滑块数值
                                    //do something
                                }
                            });

                            value = $('input[name="music_volume"]').val();
                            //音乐音量
                            slider.render({
                                elem: '#music_volume',  //绑定元素
                                min:1,
                                max:100,
                                step:1,
                                value: value!=undefined ? value : 10,
                                tips:true,
                                theme:"#2d8cf0",
                                input:true,
                                change: function(value){
                                    layui.$('input[name="music_volume"]').val(value);
                                    //console.log(value) //动态获取滑块数值
                                    //do something
                                }
                            });

                            value = $('input[name="font_size"]').val();
                            //字幕大小
                            slider.render({
                                elem: '#font_size',  //绑定元素
                                min:1,
                                max:100,
                                step:1,
                                value:value!=undefined ? value : 10,
                                tips:true,
                                theme:"#2d8cf0",
                                input:true,
                                change: function(value){
                                    layui.$('input[name="font_size"]').val(value);
                                    //console.log(value) //动态获取滑块数值
                                    //do something
                                }
                            });
                        });

                        //todo 2 字体
                        layui.$.ajax({
                            url: "/app/admin/font/index/select?status=1&limit=1000&lang=英文",
                            dataType: "json",
                            async:true,
                            headers: get_header({"status":"1","lang":"英文"}),
                            success: function (res3) {
                                let html = '';
                                let fontid = $('input[name="fontid"]').val();
                                layui.$.each(res3.data,function (i,v) {
                                    let selected = '';
                                    if(fontid==v.id){
                                        selected = 'selected_class';
                                    }
                                    html += ' <div class="font_click '+selected+'" data-id="'+v.id+'" style="width: 100px;height: 62px;margin-right: 10px;">\n' +
                                        '           <img src="'+v.icon+'" style="width: 100px;height: 42px;">\n' +
                                        '           <div class="ellipsis">'+v.name+'</div>\n' +
                                        '  </div>';
                                })
                                layui.$('.font_html').html(html);
                                if (res3.code) {
                                    layui.popup.failure(res3.msg);
                                }
                            }
                        });
                        $('.font_html').delegate('.font_click','click',function () {
                            $('.font_click').removeClass('selected_class');
                            $(this).addClass('selected_class');
                            $('input[name="fontid"]').val($(this).data('id'));
                            //console.log($(this).data('id'));
                        })

                        // 字段 创作内容 chapterid
                        // layui.use(["jquery", "xmSelect", "popup"], function() {
                        //     PART_NUMS = res.data[0]['part_nums'];
                        //     layui.$.ajax({
                        //         url: "/app/admin/novel/chapter/select?format=select&limit=8000",
                        //         dataType: "json",
                        //         async:true,
                        //         headers: get_header({"format":"select"}),
                        //         success: function (res4) {
                        //             let value = layui.$("#chapterid").attr("value");
                        //             let initValue = value ? value.split(",") : [];
                        //             layui.xmSelect.render({
                        //                 el: "#chapterid",
                        //                 name: "chapterid",
                        //                 filterable: true,
                        //                 data: res4.data,
                        //                 initValue: initValue,
                        //                 model: {"icon":"hidden","label":{"type":"text"}},
                        //                 clickClose: true,
                        //                 radio: true,
                        //                 disabled: PART_NUMS>0 ? true : false
                        //             })
                        //             if (res4.code) {
                        //                 layui.popup.failure(res4.msg);
                        //             }
                        //         }
                        //     });
                        // });

                        //音色
                        layui.use(["jquery", "xmSelect", "popup"], function() {
                            layui.$.ajax({
                                url: "/app/admin/timbre/index/select?format=tree&spread_type=3",
                                dataType: "json",
                                headers: get_header({'format':"tree","spread_type":3}),
                                success: function (res) {
                                    let value = layui.$("#timbreid").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#timbreid",
                                        name: "timbreid",
                                        initValue: initValue,
                                        tips: "请选择",
                                        //toolbar: {show: true, list: ["CLEAR"]},
                                        data: res.data,
                                        value: "0",
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                        tree: {show: true,"strict":false,"clickCheck":true,"clickExpand":true},
                                    });
                                    if (res.code) {
                                        return layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        });

                        // 字段 背景音乐 musicid
                        layui.use(["jquery", "xmSelect", "popup"], function() {
                            layui.$.ajax({
                                url: "/app/admin/music/index/select?format=select&limit=1000&spread_type=3",
                                dataType: "json",
                                async:true,
                                headers: get_header({"format":"select","spread_type":3}),
                                success: function (res) {
                                    let value = layui.$("#musicid").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#musicid",
                                        name: "musicid",
                                        filterable: true,
                                        data: res.data,
                                        initValue: initValue,
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                    })
                                    if (res.code) {
                                        layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        });

                        // 字段 状态 视频比例
                        layui.use(["jquery", "xmSelect"], function() {
                            let value = layui.$("#video_scale").attr("value");
                            let initValue = value ? value.split(",") : ["9:16"];
                            layui.xmSelect.render({
                                el: "#video_scale",
                                name: "video_scale",
                                filterable: true,
                                initValue: initValue,
                                data: [{"value":"9:16","name":"9:16"},{"value":"3:4","name":"3:4"},{"value":"4:3","name":"4:3"}],
                                model: {"icon":"hidden","label":{"type":"text"}},
                                clickClose: true,
                                radio: true
                            })
                        });

                        let settings = res.data[0]['settings'];
                        layui.$.each(settings,function (i,v) {
                            if(v==false){
                                layui.$('input[name="settings['+i+']"]').removeAttr("checked");
                            }
                        })
                        layui.form.render();

                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                        
                    }
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                layui.form.on("submit(save)", function (data) {
                    // if(data.field.chapterid==false){
                    //     layui.popup.failure('创作内容必选');
                    //     return false;
                    // }
                    if(data.field.lang==false){
                        layui.popup.failure('语言必选');
                        return false;
                    }
                    if(data.field.timbreid==false){
                        layui.popup.failure('主播音色必选');
                        return false;
                    }
                    if(data.field.musicid==false){
                        data.field.musicid = 0;
                    }
                    if(data.field.fontid==false){
                        layui.popup.failure('字体必选');
                        return false;
                    }
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    data.field = searchWhere(data.field);

                    let loading = layer.load();
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        async:true,
                        headers: get_header(data.field),
                        success: function (res) {
                            layer.close(loading);
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>

</html>
